package report

import (
	"context"
	"google.golang.org/grpc"
	report "qabx.net/safe-app-center/api/report"
	"qabx.net/safe-app-center/pkg/config/constants"
)

var trafficClient report.TrafficReceiveServiceClient

func GetEdgeSecurityClient() report.TrafficReceiveServiceClient {
	if trafficClient == nil {
		trafficClient = initClient()
	}
	return trafficClient
}

func initClient() report.TrafficReceiveServiceClient {
	conn, err := grpc.Dial(constants.REPORT_URL, grpc.WithInsecure())
	if err != nil {
		return nil
	}
	edgeSecurityClient := report.NewTrafficReceiveServiceClient(conn)
	return edgeSecurityClient
}

func CallUploadTrafficLog(log *report.TrafficLog) (*report.Response, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	client := GetEdgeSecurityClient()

	return client.UPLoadTrafficLog(ctx, log)
}
